package com.gec.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.common.R;
import com.gec.dto.DishDto;
import com.gec.entity.Dish;
import com.gec.service.CategoryService;
import com.gec.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
/**
 * <p>
 *  菜品管理
 * </p>
 *
 * @author : [chenxys]
 * @version : [v1.0]
 * @createTime : [2022/7/9 19:38]
 */
@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 菜品分页查询
     * @author chenxys
     * @param page 当前页
     * @param pageSize 每页显示条数
     * @param name 查询条件
     * @return com.gec.common.R<com.baomidou.mybatisplus.extension.plugins.pagination.Page>
     */
    @GetMapping("/page")
    public R<Page> page(Long page,Long pageSize,String name){
        Page<Dish> pageInfo = new Page<>(page,pageSize);
        Page<DishDto> dishDtoPage = (Page<DishDto>) dishService.pages(pageInfo, name);
        log.info("分页查询");
        return R.success(dishDtoPage);
    }

    /**
     * 保存菜品
     * @author chenxys
     * @param dishDto 菜品信息
     * @return com.gec.common.R<java.lang.String>
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        dishService.saveWithFlavor(dishDto);
        log.info("新增菜品成功");
        return R.success("新增菜品成功");
    }

    /**
     * 修改菜品状态
     * @author chenxys
     * @param code 状态码
     * @param ids 菜品ID
     * @return com.gec.common.R<java.lang.String>
     */
    @PostMapping("/status/{code}")
    public R<String> status(@PathVariable(value = "code",required = true) Integer code,Long[] ids){
        dishService.updateBatchByIds(code,ids);
        log.info("更新状态成功");
        return R.success("更新状态成功");
    }

    /**
     * 删除菜品
     * @author chenxys
     * @param ids 菜品ID
     * @return com.gec.common.R<java.lang.String>
     */
    @DeleteMapping
    public R<String> delete(Long[] ids){
        if (!ObjectUtils.isEmpty(ids)){
            dishService.removeByIdsWithFlavor(Arrays.asList(ids));
            log.info("删除菜品成功");
            return R.success("删除菜品成功");
        }
        return R.error("未选中菜品");
    }

    /**
     * 获取当个菜品信息
     * @author chenxys
     * @param id 菜品ID
     * @return com.gec.common.R<com.gec.dto.DishDto>
     */
    @GetMapping("/{id}")
    public R<DishDto> getOne(@PathVariable("id") Long id){
        DishDto dishDto = dishService.getByIdWithFlavor(id);
        log.info("查询菜品成功");
        return R.success(dishDto);
    }

    /**
     * 更新菜品
     * @author chenxys
     * @param dishDto 菜品信息
     * @return com.gec.common.R<java.lang.String>
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        dishService.updateByDishDto(dishDto);
        log.info("更新菜品成功");
        return R.success("更新菜品成功");
    }

    /**
     * 查询当前分类ID下所有的菜品信息
     * @author chenxys
     * @param dish 封装到dish中
     * @return com.gec.common.R<com.gec.entity.Dish>
     */
    @GetMapping("/list")
    public R<List<DishDto>> dishList(Dish dish){
        List<DishDto> dishDtoList = dishService.listByDish(dish);
        log.info("查询当前分类下所有的菜品信息");
        return R.success(dishDtoList);
    }
}
